import { Component } from 'react'
import store from '../../store'
import { myContext } from './context'

export const connect = (mapState: any, mapDispatch:any) => (Wrapper:any) => {
  return class extends Component {
    constructor(props:any) {
      super(props)
      this.state = store.getState()
      store.subscribe(()=>{
        this.setState(store.getState())
      })
    }
    componentDidMount() {
    }
    componentWillUnmount() {
      // 取消更新
    }
    render() {
      return <myContext.Consumer>
        {
          ({dispatch}) => {
            const state = mapState(this.state)
            const patch = mapDispatch(dispatch)
            console.log('我在connect中:', patch)
            return <Wrapper {...state} dispatch={patch.dispatch}></Wrapper>
          }
        }
      </myContext.Consumer>
    }
  }
}
export default connect;
